﻿// This source file is part of the atomdb project
// 
// Copyright (c) 2017 - 2022 pulsarware, All rights reserved.
// Copyright (c) 2017 - 2022 polarboy <polarboy@163.com>
// 
// See https://atomdb.com/LICENSE.txt for license information
// 
// Created by polarboy on 2023/12/01.

using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Pulsarware.ATOMDB.Packager;

var consoleHostBuilder = Host.CreateApplicationBuilder(args);
consoleHostBuilder.Configuration.Sources.Clear();

var env = consoleHostBuilder.Environment;
var homeDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
var configFilePathParts = new string[]{homeDir, ".atomdb-packager", "appsettings.json"};
var atomdbHomeConfigFilePath = Path.Combine(configFilePathParts);

// 在这里我们配置的查找顺序
// 1、程序当前的运行目录
// 2、用户当前的家目录
// 3、命令行直接指定
consoleHostBuilder.Configuration
   .AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
   .AddJsonFile($"appsettings.{env.EnvironmentName}.json", true, false)
   .AddJsonFile(atomdbHomeConfigFilePath, true, false);

consoleHostBuilder.Services.AddOptions<Config>()
   .Bind(consoleHostBuilder.Configuration.GetSection("ATOMDBPackager"));

consoleHostBuilder.Services.AddPackagerServices();

using var consoleHost = consoleHostBuilder.Build();
var startup = (StartupService)consoleHost.Services.GetService(typeof(StartupService))!;
await startup.ExecuteAsync(args);